import requests
import re
import json
import random
import multiprocessing
import threading

lock = threading.Lock()
pool = multiprocessing.Pool(6)
class Spider:
    def __init__(self):
        self.url = 'https://www.douyu.com/gapi/rknc/directory/yzRec/'


    def get_urls(self):
        return [self.url + str(i) for i in range(1, 6)]


    def down_img(self, response, name):
        lock.acquire()
        with open('/home/python/Desktop/斗鱼img/' + str(random.randrange(1, 10000, 4)) + name, 'wb') as f:
            f.write(response.content)
            print('{}格式保存成功'.format(name))
        lock.release()


    def start_request(self):
        for url in self.get_urls():
            response = requests.get(url)
            json_str = json.dumps(response.text)
            imgs_url = re.findall('"rs16":"(.*?)"', response.text)
            for img_url in imgs_url:
                img_name = img_url.split('.')[-1]
                print(img_name)
                response = requests.get(img_url)
                self.down_img(response, img_name)
                # threading.Thread(target=self.down_img, args=(response, img_name)).start()
            print(1)


if __name__ == '__main__':
    spider = Spider()
    spider.start_request()
